package scuts.core;

import scuts.core.types.LazyIterator;


class LastArray
{
  public static function last<T>(arr:Array<T>):T
  {
    if (arr.length == 0) throw "cannot get last from empty array";
    return arr[arr.length - 1];
  } 
}
 
class LastIterable
{
  public static function last<T>(iter:Iterable<T>):T
  {
    return LastIterator.last(iter.iterator());
  }
}

class LastIterator
{
  public static function last<T>(iter:Iterator<T>):T
  {
    if (!iter.hasNext()) throw "cannot get last from empty iterable";
    var last = iter.next();
    for (e in iter) {
      last = e;
    }
    return last;
  } 
}

class LastLazyIterator
{
  public static function last<T>(iter:LazyIterator<T>):T
  {
    return LastIterator.last(iter());
  } 
}